package com.itec.learn.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/1/12 0012.
 */
public class Server {

    public static void main(String[] args) throws IOException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        ServerSocket serverSocket = new ServerSocket(9999);
        while (!Thread.currentThread().isInterrupted()) {
            Socket socket = serverSocket.accept();
            executor.submit(new ConnectIOHandler(socket));
        }
    }

}

class ConnectIOHandler extends Thread {
    private Socket socket;

    public ConnectIOHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted() && !socket.isClosed()) {
            try {
                InputStream in = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String data = null;
                while ((data = br.readLine()) != null){
                    System.out.println(data);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
